package com.ruoyi.drawing.controller;

import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.drawing.domain.DtsDrawingModelRegion;
import com.ruoyi.drawing.api.params.DrawingModelRegionParams;
import com.ruoyi.drawing.api.vo.DrawingModelRegionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.drawing.service.IDtsDrawingModelRegionService;
import com.ruoyi.common.core.web.controller.BaseController;

import java.util.List;

/**
 * 图档中心模型区域
 *
 * @author ruoyi
 * @date 2024-04-29
 */
@Api(tags = "图档中心模型区域")
@RestController
@RequestMapping("/region")
public class DtsDrawingModelRegionController extends BaseController {

    @Resource
    private IDtsDrawingModelRegionService dtsDrawingModelRegionService;

    /**
     * 图档中心模型区域列表
     *
     * @return 图档中心模型区域列表
     */
    @ApiOperation("图档中心模型区域列表")
    @GetMapping("/tree")
    public AjaxResult regionTree() {
        List<DrawingModelRegionVO> drawingModelRegionList = dtsDrawingModelRegionService.regionTree();
        return AjaxResult.success(drawingModelRegionList);
    }

    /**
     * 新增图档中心模型区域
     *
     * @param drawingModelRegionParams 图档中心模型区域新增or修改参数
     * @return 新增信息
     */
    @ApiOperation("新增图档中心模型区域")
    @PostMapping("/add")
    public AjaxResult addDrawingModelRegion(@RequestBody DrawingModelRegionParams drawingModelRegionParams) {
        dtsDrawingModelRegionService.addDrawingModelRegion(drawingModelRegionParams);
        return AjaxResult.success();
    }

    /**
     * 修改图档中心模型区域
     *
     * @param drawingModelRegionParams 图档中心模型区域新增or修改参数
     * @return 修改信息
     */
    @ApiOperation("修改图档中心模型区域")
    @PutMapping("/update")
    public AjaxResult updateDrawingModelRegion(@RequestBody DrawingModelRegionParams drawingModelRegionParams) {
        if (ObjectUtil.isEmpty(drawingModelRegionParams.getId())) {
            return AjaxResult.error("id不能为空");
        }
        dtsDrawingModelRegionService.updateDrawingModelRegion(drawingModelRegionParams);
        return AjaxResult.success();
    }

    /**
     * 删除图档中心模型区域
     *
     * @param id 唯一主键
     * @return 修改信息
     */
    @ApiOperation("删除图档中心模型区域")
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestParam Long id) {
        long childCount = dtsDrawingModelRegionService.count(new QueryWrapper<DtsDrawingModelRegion>().lambda().eq(DtsDrawingModelRegion::getParentId, id));
        if (childCount > 0) {
            return AjaxResult.error("存在下级，不能删除");
        }
        dtsDrawingModelRegionService.removeById(id);
        return AjaxResult.success();
    }

}
